// 导出所有验证器
export * from './common';
export * from './user';
export * from './middleware';

// 重新导出 Joi 以便统一使用
export { default as Joi } from 'joi';

/**
 * 验证器工具函数
 */
export class ValidatorUtils {
  /**
   * 手动验证数据
   */
  static async validateData<T>(
    data: unknown,
    schema: import('joi').ObjectSchema
  ): Promise<{ isValid: boolean; value?: T; error?: string }> {
    try {
      const { error, value } = schema.validate(data, {
        allowUnknown: false,
        stripUnknown: true,
        abortEarly: false,
      });

      if (error) {
        const errorMessage = error.details
          .map(detail => detail.message)
          .join('; ');
        
        return {
          isValid: false,
          error: errorMessage,
        };
      }

      return {
        isValid: true,
        value: value as T,
      };
    } catch {
      return {
        isValid: false,
        error: '数据验证失败',
      };
    }
  }

  /**
   * 异步验证数据（Promise 版本）
   */
  static validateDataAsync<T>(
    data: unknown,
    schema: import('joi').ObjectSchema
  ): Promise<T> {
    return new Promise((resolve, reject) => {
      const { error, value } = schema.validate(data, {
        allowUnknown: false,
        stripUnknown: true,
        abortEarly: false,
      });

      if (error) {
        const errorMessage = error.details
          .map(detail => detail.message)
          .join('; ');
        reject(new Error(errorMessage));
      } else {
        resolve(value as T);
      }
    });
  }

  /**
   * 批量验证
   */
  static async validateBatch(
    validations: Array<{
      data: unknown;
      schema: import('joi').ObjectSchema;
      name: string;
    }>
  ): Promise<{
    isValid: boolean;
    results: Array<{ name: string; isValid: boolean; error?: string; value?: unknown }>;
  }> {
    const results = [];
    let overallValid = true;

    for (const validation of validations) {
      const result = await this.validateData(validation.data, validation.schema);
      results.push({
        name: validation.name,
        isValid: result.isValid,
        error: result.error,
        value: result.value,
      });

      if (!result.isValid) {
        overallValid = false;
      }
    }

    return {
      isValid: overallValid,
      results,
    };
  }

  /**
   * 创建动态验证模式
   */
  static createDynamicSchema(fields: Array<{
    name: string;
    type: 'string' | 'number' | 'boolean' | 'date' | 'email' | 'url';
    required?: boolean;
    min?: number;
    max?: number;
    pattern?: RegExp;
    valid?: unknown[];
  }>): import('joi').ObjectSchema {
    const Joi = require('joi');
    const schemaObject: Record<string, import('joi').Schema> = {};

    for (const field of fields) {
      let schema: import('joi').Schema;

      // 根据类型创建基础 schema
      switch (field.type) {
        case 'string':
          schema = Joi.string();
          break;
        case 'number':
          schema = Joi.number();
          break;
        case 'boolean':
          schema = Joi.boolean();
          break;
        case 'date':
          schema = Joi.date();
          break;
        case 'email':
          schema = Joi.string().email();
          break;
        case 'url':
          schema = Joi.string().uri();
          break;
        default:
          schema = Joi.string();
      }

      // 应用附加验证规则
      if (field.required) {
        schema = schema.required();
      }

      if (field.min !== undefined) {
        schema = (schema as import('joi').StringSchema | import('joi').NumberSchema).min(field.min);
      }

      if (field.max !== undefined) {
        schema = (schema as import('joi').StringSchema | import('joi').NumberSchema).max(field.max);
      }

      if (field.pattern) {
        schema = (schema as import('joi').StringSchema).pattern(field.pattern);
      }

      if (field.valid) {
        schema = schema.valid(...field.valid);
      }

      schemaObject[field.name] = schema;
    }

    return Joi.object(schemaObject);
  }
}